<?php
session_start();

$resource = $_REQUEST["resource"];
//echo $resource;
//require_once("includes/connect.php");
//require_once("includes/function.php");


$db = NEW myDBS();
//$db->selectDB("account");

//$sql = "SELECT DISTINCT tbl_pet.*, panngan2_name, panngan3_name, expenses1_name, expenses2_name, credit_name
//                    FROM tbl_pet, panngan2, panngan3, expenses_lvl1, expenses_lvl2, tbl_creditor 
//                    WHERE lvl1_id = '" . $_SESSION["lvl1_id"] . "'
//                    AND tbl_pet.panngan2_id = panngan2.panngan2_id
//                    AND tbl_pet.panngan3_id = panngan3.panngan3_id
//                    AND tbl_pet.ex1_id = expenses_lvl1.expenses1_id
//                    AND tbl_pet.ex2_id = expenses_lvl2.expenses2_id
//                    AND tbl_pet.receiver = tbl_creditor.creditor_id
//                    ORDER BY tbl_pet.id desc
//			";
//lvl1_id = '" . $_SESSION["lvl1_id"] . "'
 $sql = "SELECT DISTINCT tbl_pet.*, expenses1_name, expenses2_name, panngan2_name, panngan3_name, vender_name  "
                . " FROM  expenses_lvl1, expenses_lvl2, panngan2, panngan3, v_vender, tbl_pet "
                . " LEFT JOIN expenses_lvl3 ON expenses_lvl3.expenses3_id = ex3_id "
                . " WHERE  "
                . " expenses_lvl1.expenses1_id = ex1_id "
                . " AND expenses_lvl2.expenses2_id = ex2_id "
                . " AND panngan2.panngan2_id = tbl_pet.panngan2_id "
                . " AND panngan3.panngan3_id = tbl_pet.panngan3_id "
                . " AND tbl_pet.receiver = v_vender.vender_id "
                . " ORDER BY tbl_pet.id DESC ";
 

//echo $sql;
$result = $db->runSQL($sql);
//$rows = $db1->fetch_object($result);
?>

<script type="text/javascript">
    $(document).ready(function() {
        $("#example").dataTable({
            "aaSorting": []
        });

    });

    function edit(utility_id) {
        //alert("adfs");
        $('<div>').dialog2({
            autoOpen: true,
            title: "แก้ไขฎีกา",
            content: "dlg_add_pet.php?utility_id=" + utility_id,
            id: "modal-chkJob"
        });

    }

    function del(utility_id) {
        //alert("adfs");
        if (confirm("คุณแน่ใจที่ต้องการลบรายการนี้ใช่ไหม?"))
        {
            $.post("process_utility.php", {utility_id: utility_id, task: 'del'}, function() {
                window.location.href = "index.php?pages=frm_petition_utility&pageid=9";
            });
        }

    }

    function cancel(id) {
        confirm("คุณแน่ใจใช่ไหมที่ต้องการ ยกเลิก สัญญานี้");
//        if (confirm("คุณแน่ใจใช่ไหมที่ต้องการ ยกเลิก สัญญานี้")) {
//
//        $('<div>').dialog2({
//            autoOpen: true,
//            title: "ยกเลิกฎีกา",
//            content: "dlg_cancel.php?tblname=tbl_pet&id=" + id,
//            id: "modal-cancel"
//        });
//        }
    }

    function approvedPetition(id) {
        var task = "approved";

        $('<div>').dialog2({
            autoOpen: true,
            title: "อนุมัติ",
            content: "dlg_approved_pet.php?id=" + id + "&task=" + task,
            id: "modal-date"
        });


    }

    function confirm(id) {
        var task = "confirm";

        $.post('pet_status_process.php', {task: task, id: id}, function(data) {
            window.location.href = "index.php?pages=pet";
        });

    }

    function disapprovalPetition(id) {
        var task = "disapproval";

        $.post('pet_status_process.php', {task: task, id: id}, function(data) {
            window.location.href = "index.php?pages=pet";
        });

    }
</script>
<div >
    <h3>
        <p class="text-center">
            <?php
            switch ($resource) {
                case "IN":
                    echo 'เงินในงบประมาณ';

                    break;
                case "OUT":
                    echo 'เงินนอกงบประมาณ';

                    break;
                case "COLLECT":
                    echo 'เงินสะสม';

                    break;                
                default:
                    break;
            }
            ?>
        </p>
    </h3>
</div>

<a class="btn btn-primary" href="index.php?pages=pet_edit">เพิ่มฎีกา</a><hr>
<table id="example" class="table table-bordered" width="100%" >
    <caption><br></caption>
    <thead>
        <tr>
            <th>#</th>
            <th>เลขที่ฎีกา</th>
            <th>วันที่</th>
            <!--<th>แผนงาน</th>-->
            <!--<th>งาน</th>-->
            <th>หมวด</th>
            <th>ประเภท</th>
            <th>จำนวนเงิน</th>
            <th>ชื่อผู้รับเงิน</th>
            <th>action</th>
            <th>ยืนยัน</th>
            <th>สถานะฎีกา</th>
        </tr>
    </thead>
    <tbody>	
        <?php
        $i = 0;
        while ($rows = $db->fetch_object($result)) {
            $i++;
            ?>
            <tr class="<?php
        if ($rows->status == "ยกเลิก") {
            echo " error";
        } else if ($rows->confirm == "ยืนยัน") {
            //echo " success";
        }
            ?>">
                <td><p class="text-center"><?php echo $i; ?></p></td>
                <td><p class="text-center"><?php echo $rows->pet_id; ?></p></td>
                <td><p class="text-center"><?php echo $rows->date_create; ?></p></td>
                <!--<td><?php // echo $rows->panngan2_name;     ?></td>-->
                <!--<td><?php // echo $rows->panngan3_name;     ?></td>-->
                <td><?php echo $rows->expenses1_name; ?></td>
                <td><?php echo $rows->expenses2_name; ?></td>
                <td><p class="text-right"><?php echo number_format($rows->net, 2); ?></p></td>
                <td><p class="text-left"><?php echo $rows->vender_name; ?></p></td>
                <td>
                    <p class="text-center">
                        <a href='index.php?pages=pet_edit&id=<?php echo $rows->id; ?>'><i class='icon-pencil'></i></a>
                        &nbsp;|&nbsp;<a href='javascript:cancel(<?php echo $rows->id; ?>)'><i class='icon-remove'></i></a>
                        &nbsp;|&nbsp;<a href='pet_print.php?id=<?php echo $rows->id; ?>' target='_blank'><i class='icon-print'></i></a>
                    </p>
                </td>
                <?php
                echo "<td><p class='text-center'>";
                if ($rows->confirm == "รอยืนยัน") {
                    echo "<a href='javascript:confirm(\"$rows->id\")' data-toggle='tooltip' title='ยืนยัน'><i class='icon-ok'></i></a>";
                } else {
                    echo $rows->confirm;
                }
//                    if ($rows->approved_status == "รออนุมัติ") {
//                        echo "<a href='javascript:approvedPetition(\"$rows->id\")' data-toggle='tooltip' title='อนุมัติ'><i class='icon-ok'></i></a>&nbsp;<big>|</big>&nbsp;<a href='javascript:disapprovalPetition(\"$rows->id\")' data-toggle='tooltip' title='ไม่อนุมัติ'><i class='icon-remove'></i></a>";
//                    } else {
//                        if ($rows->approved_status == "อนุมัติ") {
//                            echo "<font color='blue'>";
//                        }
//                        echo $rows->approved_status;
//                    }
//                } else {
//                    echo "รอการตรวจสอบ";
//                }
                echo "</p></td>";
                ?>
                <td><p class="text-center"><?php echo $rows->status; ?></p></td>
            </tr>
            <?php
        }
        ?>

    </tbody>
</table>
